package com.happy3w.ssp.schoolmatehome.controller;

import com.happy3w.ssp.schoolmatehome.interceptor.AttributeKeys;
import com.happy3w.ssp.schoolmatehome.service.auth.SessionInfo;
import com.happy3w.ssp.schoolmatehome.service.auth.SessionService;
import com.happy3w.ssp.schoolmatehome.service.auth.TheUserDetail;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/session")
public class SessionController {
    private final SessionService sessionService;

    @PostMapping("/prepare-login-mini")
    public ResponseEntity<?> prepareLoginMini(@RequestBody PrepareLoginMiniRequest req) {
        if (!StringUtils.hasText(req.id)) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body("code不能为空");
        }
        String jumpCode = sessionService.prepareLoginMini(req.id);
        return ResponseEntity.ok(Map.of("jumpCode", jumpCode));
    }

    @Data
    public static class PrepareLoginMiniRequest {
        private String id;
    }


    @PostMapping("/login-mini")
    public ResponseEntity<?> loginMini(@RequestBody LoginMiniRequest req) {
        if (StringUtils.hasText(req.code)) {
            SessionInfo result = sessionService.loginMini(req.code, req.nickName, req.avatarUrl, req.jumpCode);
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body("code不能为空");
        }
    }

    @PostMapping("/check-login-status")
    public ResponseEntity<?> checkLoginStatus(@RequestBody CheckLoginStatusRequest req) {
        if (!StringUtils.hasText(req.jumpCode)) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body("jumpCode不能为空");
        }
        SessionInfo sessionInfo = sessionService.checkLoginStatus(req.jumpCode);
        if (sessionInfo == null) {
            sessionInfo = SessionInfo.builder()
                    .jumpCode(req.jumpCode)
                    .build();
        }
        return ResponseEntity.ok(sessionInfo);
    }

    @Data
    public static class CheckLoginStatusRequest {
        private String jumpCode;
    }

    @Data
    public static class LoginMiniRequest {
        private String code;
        private String nickName;
        private String avatarUrl;
        private String jumpCode;
    }


    @PostMapping("/login")
    public ResponseEntity<?> loginWeb(@RequestBody LoginRequestWeb req) {
        if (StringUtils.hasText(req.code)) {
            SessionInfo result = sessionService.loginByCode(req.code);
            return ResponseEntity.ok(result);
        } else if (StringUtils.hasText(req.phone)) {
            SessionInfo result = sessionService.loginByPhone(req.phone, req.password);
            return ResponseEntity.ok(result);
        } else if (StringUtils.hasText(req.email)) {
            SessionInfo result = sessionService.loginByEmail(req.email, req.password);
            return ResponseEntity.ok(result);
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body("code或phone不能为空");
        }
    }

    @PostMapping("/renew")
    public ResponseEntity<?> renew(@RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        SessionInfo result = sessionService.renew(userDetail.getUser());
        return ResponseEntity.ok(result);
    }

    @Data
    public static class LoginRequestWeb {
        private String code;
        private String phone;
        private String email;
        private String password;
    }

}
